home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP10 / MENUDEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  4.9 KB  |  138 lines

  1. /*-----------------------------------------
  2.    MENUDEMO.C -- Menu Demonstration
  3.                  (c) Charles Petzold, 1996
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "menudemo.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. char szAppName[] = "MenuDemo" ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15.      {
  16.      HWND       hwnd ;
  17.      MSG        msg ;
  18.      WNDCLASSEX wndclass ;
  19.  
  20.      wndclass.cbSize        = sizeof (wndclass) ;
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = szAppName ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  32.  
  33.      RegisterClassEx (&wndclass) ;
  34.  
  35.      hwnd = CreateWindow (szAppName, "Menu Demonstration",
  36.                           WS_OVERLAPPEDWINDOW,
  37.                           CW_USEDEFAULT, CW_USEDEFAULT,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           NULL, NULL, hInstance, NULL) ;
  40.  
  41.      ShowWindow (hwnd, iCmdShow) ;
  42.      UpdateWindow (hwnd) ;
  43.  
  44.      while (GetMessage (&msg, NULL, 0, 0))
  45.           {
  46.           TranslateMessage (&msg) ;
  47.           DispatchMessage (&msg) ;
  48.           }
  49.      return msg.wParam ;
  50.      }
  51.  
  52. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  53.      {
  54.      static int  iColorID[5] = { WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,
  55.                                  DKGRAY_BRUSH, BLACK_BRUSH } ;
  56.      static int  iSelection = IDM_WHITE ;
  57.      HMENU       hMenu ;
  58.  
  59.      switch (iMsg)
  60.           {
  61.           case WM_COMMAND :
  62.                hMenu = GetMenu (hwnd) ;
  63.  
  64.                switch (LOWORD (wParam))
  65.                     {
  66.                     case IDM_NEW :
  67.                     case IDM_OPEN :
  68.                     case IDM_SAVE :
  69.                     case IDM_SAVEAS :
  70.                          MessageBeep (0) ;
  71.                          return 0 ;
  72.  
  73.                     case IDM_EXIT :
  74.                          SendMessage (hwnd, WM_CLOSE, 0, 0L) ;
  75.                          return 0 ;
  76.  
  77.                     case IDM_UNDO :
  78.                     case IDM_CUT :
  79.                     case IDM_COPY :
  80.                     case IDM_PASTE :
  81.                     case IDM_DEL :
  82.                          MessageBeep (0) ;
  83.                          return 0 ;
  84.  
  85.                     case IDM_WHITE :          // Note: Logic below
  86.                     case IDM_LTGRAY :         //   assumes that IDM_WHITE
  87.                     case IDM_GRAY :           //   through IDM_BLACK are
  88.                     case IDM_DKGRAY :         //   consecutive numbers in
  89.                     case IDM_BLACK :          //   the order shown here.
  90.  
  91.                          CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
  92.                          iSelection = LOWORD (wParam) ;
  93.                          CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
  94.      
  95.                          SetClassLong (hwnd, GCL_HBRBACKGROUND,
  96.                               (LONG) GetStockObject 
  97.                                  (iColorID[LOWORD (wParam) - IDM_WHITE])) ;
  98.  
  99.                          InvalidateRect (hwnd, NULL, TRUE) ;
  100.                          return 0 ;
  101.  
  102.                     case IDM_START :
  103.                          if (SetTimer (hwnd, 1, 1000, NULL))
  104.                               {
  105.                               EnableMenuItem (hMenu, IDM_START, MF_GRAYED) ;
  106.                               EnableMenuItem (hMenu, IDM_STOP,  MF_ENABLED) ;
  107.                               }
  108.                          return 0 ;
  109.  
  110.                     case IDM_STOP :
  111.                          KillTimer (hwnd, 1) ;
  112.                          EnableMenuItem (hMenu, IDM_START, MF_ENABLED) ;
  113.                          EnableMenuItem (hMenu, IDM_STOP,  MF_GRAYED) ;
  114.                          return 0 ;
  115.  
  116.                     case IDM_HELP :
  117.                          MessageBox (hwnd, "Help not yet implemented!",
  118.                                      szAppName, MB_ICONEXCLAMATION | MB_OK) ;
  119.                          return 0 ;
  120.  
  121.                     case IDM_ABOUT :
  122.                          MessageBox (hwnd, "Menu Demonstration Program.",
  123.                                      szAppName, MB_ICONINFORMATION | MB_OK) ;
  124.                          return 0 ;
  125.                     }
  126.                break ;
  127.  
  128.           case WM_TIMER :
  129.                MessageBeep (0) ;
  130.                return 0 ;
  131.  
  132.           case WM_DESTROY :
  133.                PostQuitMessage (0) ;
  134.                return 0 ;
  135.           }
  136.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  137.      }
  138.